<?php

//Created by Timothy

class details_model extends CI_Model {

    var $bktitle = "";
    var $author = "";
    var $callnumber = "";
    var $edition = "";
    var $publisher = "";
    var $desc = "";
    var $bkformat = "";
    var $isbn13 = "";
    var $isbn13_l = "";
    var $isbn10 = "";
    var $issn = "";
    var $summary = "";
    var $frequency = "";
    var $eBkLink = "";
    var $docnum = "";
    var $img_link = "";

    function __construct() {
        parent::__construct();
    }

 /*
|--------------------------------------------------------------------------
| parseDetailsXML (@author : Timothy Cheung)
|--------------------------------------------------------------------------
|
| This function takes the XML returned from X-Services API as input, parses 
| and returns a details record for the Details View module
| For information on the purpose of the variables, please check
| app/view/DetailsViewModule/MainTabDataView.js's comments
*/   
    
    function parseDetailsXML($xmlDetails) {
        $bktitle = "";
        $author = "";
        $callnumber = "";
        $edition = "-";
        $publisher = "";
        $desc = "";
        $format_ff_FMT = "";
        $isbn1 = "";
        $isbn1_l = "";
        $isbn2 = "";
        $isbn2_l = "";
        $issn = "";
        $summary = "";
        $frequency = "";
        $eBkLink = "";
        $docnum = "";

        foreach ($xmlDetails->children() as $child) {
            $item = new details_model();
            $count = 0;
            $tempstr = "";

            if ($child->getName() == "record") {
                foreach ($child->children() as $item_data) {
                    switch ($item_data->getName()) {
                        case "doc_number":
                            $docnum = (string) $item_data;                 //item_data==metadata
                            break;
                        case "metadata":
                            foreach ($item_data->children() as $sdata) {     //item_data==metadata, sdata==oai_marc
                                foreach ($sdata->children() as $subdata) {   //sdata==oai_marc, subdata==fixfield/varfield
                                    switch ($subdata->getName()) {
                                        case "fixfield":
                                            if ($subdata->attributes() == "FMT") {  //Format: <metadata>,<oai_marc>,<fixfield id='FMT'>
                                                $format_ff_FMT = (string) $subdata;
                                            }
                                            break;

                                        case "varfield":

                                            switch ($subdata->attributes()) {
                                                case "020": //ISBN10 & ISBN13: <metadata>,<oai_marc>,<varfield id="020">,<subfield label="a">
                                                    if ($count == 0) {
                                                        foreach ($subdata->children() as $subfield) {
                                                            if ($subfield->attributes() == "a") {
                                                                $isbn1_l = (string) $subfield;  //original full isbn string
                                                                $isbn1 = (string) preg_replace("/[^0-9]/", "", $subfield); //remove all non-numericals from ISBN
                                                                $count++;
                                                            }
                                                        }
                                                    } elseif ($count == 1) {
                                                        foreach ($subdata->children() as $subfield) {
                                                            if ($subfield->attributes() == "a")
                                                                $isbn2_l = (string) $subfield;
                                                            $isbn2 = (string) preg_replace("/[^0-9]/", "", $subfield);
                                                        }
                                                    }
                                                    break;

                                                case "022": //ISSN: <metadata>,<oai_marc>,<varfield id="022">,<subfield label="a">
                                                    foreach ($subdata->children() as $subfield) {
                                                        if ($subfield->attributes() == "a")
                                                            $issn = (string) $subfield;
                                                    }
                                                    break;
                                                case "099": //Call Number: <metadata>,<oai_marc>,<varfield id="099">,<subfield label="a">
                                                    foreach ($subdata->children() as $subfield) {
                                                        if ($subfield->attributes() == "a")
                                                            $callnumber = (string) $subfield;
                                                    }
                                                    break;
                                                case "100": //Author: <metadata>,<oai_marc>,<varfield id="100">,<subfield label="a">
                                                    foreach ($subdata->children() as $subfield) {
                                                        if ($subfield->attributes() == "a")
                                                            $author .= (string) $subfield;
                                                    }
                                                    break;
                                                case "245": //Title: <metadata>,<oai_marc>,<varfield id="245">
                                                    foreach ($subdata->children() as $subfield) {
                                                        $tempstr .= (string) $subfield;
                                                    }
                                                    $bktitle .= $tempstr;
                                                    $tempstr = "";
                                                    break;
                                                case "250": //Edition: <metadata>,<oai_marc>,<varfield id="250">
                                                    foreach ($subdata->children() as $subfield) {
                                                        $tempstr .= (string) $subfield;
                                                    }
                                                    $edition = $tempstr;
                                                    $tempstr = "";
                                                    break;
                                                case "260": //Publisher: <metadata>,<oai_marc>,<varfield id="260">
                                                    foreach ($subdata->children() as $subfield) {
                                                        $tempstr .= (string) $subfield;
                                                    }
                                                    $publisher .= $tempstr;
                                                    $tempstr = "";
                                                    break;
                                                case "300": //Description: <metadata>,<oai_marc>,<varfield id="300">
                                                    foreach ($subdata->children() as $subfield) {
                                                        $tempstr .= (string) $subfield;
                                                    }
                                                    $desc .= $tempstr;
                                                    $tempstr = "";
                                                    break;
                                                case "520": //Summary: <metadata>,<oai_marc>,<varfield id="520">
                                                    foreach ($subdata->children() as $subfield) {
                                                        $tempstr .= (string) $subfield;
                                                    }
                                                    $summary .= $tempstr;
                                                    $tempstr = "";
                                                    break;

                                                case "310": //Frequency: <metadata>,<oai_marc>,<varfield id="310">,<subfield label="a">
                                                    if ($format_ff_FMT == "SE") {
                                                        foreach ($subdata->children() as $subfield) {
                                                            if ($subfield->attributes() == "a") {
                                                                $tempstr .= (string) $subfield;
                                                            }
                                                        }
                                                        $frequency .= $tempstr;
                                                        $tempstr = "";
                                                    }
                                                    break;
                                                case "856": 
                                                    //eBook electronic location(url and link text): <metadata>,<oai_marc>,<varfield id="310">,<subfield label="u">/<subfield label="z">
                                                    //retrieve electronic location if (format is BK AND title contains 'ebook') OR (format is SE AND title contains 'ejournal')
                                                    if (($format_ff_FMT == "BK" && strpos(strtolower($bktitle), "ebook"))||($format_ff_FMT == "SE" && strpos(strtolower($bktitle), "ejournal"))) {
                                                        $url = "";
                                                        $link_txt = "";
                                                        foreach ($subdata->children() as $subfield) {
                                                            if ($subfield->attributes() == "u") {
                                                                $url = (string) $subfield;
                                                            } else if ($subfield->attributes() == "z") {
                                                                $link_txt = (string) $subfield;
                                                            }
                                                        }
                                                        $tempstr = "<a href='" . $url . "' target='_blank'><button class='ebkBtn'>" . $link_txt . "</button></a>";
                                                        $eBkLink .= $tempstr;
                                                        $tempstr = "";
                                                    }
                                            }
                                    }
                                }
                            }
                            break;
                    }
                }
            }

            $item = new details_model();
            $item->docnum = $docnum;
            $item->callnumber = $callnumber;

            $item->author = $author;
            $item->bktitle = $bktitle;
            $item->publisher = $publisher;
            $item->edition = $edition;

            if ($desc != "") {
                $item->desc = $desc;
            } else {
                $item->desc = "-";
            }

            $item->bkformat = $format_ff_FMT;

            if ($summary != "") {
                $item->summary = $summary;
            } else {
                $item->summary = "-";
            }
            //Assign longer isbn string as ISBN13, shorter as ISBN10
            if (strlen($isbn1) > strlen($isbn2)) {
                $item->isbn13 = $isbn1;
                $item->isbn13_l = $isbn1_l;
                $item->isbn10 = $isbn2_l;
            } else if (strlen($isbn1) < strlen($isbn2)) {
                $item->isbn13 = $isbn2;
                $item->isbn13_l = $isbn2_l;
                $item->isbn10 = $isbn1_l;
            } else {
                $item->isbn13 = $isbn1;
                $item->isbn13_l = $isbn1_l;
            }
            //@Config: Details model static data 'issn'
            if ($issn != "") {
                $item->issn = $this->config->item('issn') . "<h4>" . $issn . "</h4>";
            } else {

                $item->issn = '</div>';
            }

            //@Config: Details model static data 'freq'
            if ($frequency != "") {
                $item->frequency = $this->config->item('freq') . "<h4>" . $frequency . "</h4></div>";
            } else {

                $item->frequency = "";
            }
            //@Config: Details model static data 'div'
            if ($eBkLink != "") {
                $item->eBkLink = $eBkLink . "</div>";
            } else {
                $item->eBkLink = (string) $this->config->item('div');
            }
            //Generates img tag to display thumbnail
            //@Config: Details model static data 'img1', 'img2'
            if ($item->isbn13 != "") {
                $item->img_link = "<img id='mtImg' src='http://lib.syndetics.com/index.aspx?isbn=".$item->isbn13 . "/SC.GIF&client=tpoly&type=xw12'>";
                
            }
        }

        $itemDataList = array('Details' => $item);
        return $itemDataList;
    }
}

?>
